package com.zkingsoft.services.distribution.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkingsoft.dao.distribution.DisCouponRuleDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.distribution.DisCouponRule;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisCouponRuleService;
import com.zkingsoft.util.ModelUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-07-03 03:35
 */
@Service("disCouponRuleService")
public class DisCouponRuleServiceImpl implements DisCouponRuleService {

	@Autowired
	private DisCouponRuleDao disCouponRuleDao;

	@Override
	public int add(DisCouponRule disCouponRule) {

		return disCouponRuleDao.insert(disCouponRule);

	}

	@Override
	public int batchAdd(List<DisCouponRule> disCouponRuleList) {
		int num = 0;

		int c = 10000;
		int size = disCouponRuleList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= disCouponRuleList.size() ? disCouponRuleList.size() : end;
			List<DisCouponRule> insertList = disCouponRuleList.subList(begin, end);
			num += disCouponRuleDao.batchInsert(insertList);
		}
		return num;

	}

	@Override
	public int modifyByMap(DisCouponRule oldDisCouponRule, DisCouponRule newDisCouponRule) {

		if (!ModelUtils.isModified(oldDisCouponRule, newDisCouponRule)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDisCouponRule, newDisCouponRule);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("couponRuleId", oldDisCouponRule.getCouponRuleId());
			disCouponRuleDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(DisCouponRule disCouponRule) {

		return disCouponRuleDao.updateByModel(disCouponRule);

	}

	@Override
	public int remove(List<Long> list) {

		return disCouponRuleDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long couponRuleId) {

		return disCouponRuleDao.deleteById(couponRuleId);

	}

	@Override
	public int removeByModel(DisCouponRule disCouponRule) {

		return disCouponRuleDao.deleteByModel(disCouponRule);

	}

	@Override
	public List<DisCouponRule> findInPage(DisCouponRule disCouponRule, PaginationVO pageVo) {

		return disCouponRuleDao.selectInPage(disCouponRule, pageVo);

	}

	@Override
	public List<DisCouponRule> findByModel(DisCouponRule disCouponRule) {

		return disCouponRuleDao.selectByModel(disCouponRule);

	}

	@Override
	public int findTotal(DisCouponRule disCouponRule) {

		return disCouponRuleDao.selectTotalRecord(disCouponRule);

	}

	@Override
	public DisCouponRule findById(Long couponRuleId) {

		return disCouponRuleDao.selectById(couponRuleId);

	}
	
	@Override
	public List<DisCouponRule> getAllRules() {
		
		return disCouponRuleDao.getAllRules();
	}

}